package com.ai.bitcoin.schedule.task;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import com.ai.bitcoin.jpa.entity.BccPriceDay;
import com.ai.bitcoin.jpa.entity.BccPriceInfo;
import com.ai.bitcoin.jpa.entity.BccShowControl;
import com.ai.bitcoin.jpa.service.*;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.serialize.BeanSerial;
import com.ai.bitcoin.thread.pool.TaskExecutor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;

/**
 * @author Jason
 * @date 2018/4/6
 **/
@Slf4j
@Component
public class DayPriceTask extends AbstractTask {
    @Autowired
    BccPriceInfoService bccPriceInfoService;
    @Autowired
    BccPriceDayService bccPriceDayService;
    @Autowired
    BccBatchService bccBatchService;
    @Autowired
    BccBasicInfoService bccBasicInfoService;
    @Autowired
    BccPriceInfoCrawlService bccPriceInfoCrawlService;
    @Autowired
    BccRedisService bccRedisService;
    @Autowired
    BccShowControlService bccShowControlService;
    @Autowired
    TaskExecutor taskExecutor;

    @Value("${priceNodeNumDay}")
    int nodeNumDay = 24;//每天产生几个节点

    @Override
    public void execute() {
        log.info("dayPriceTask start");
        long start = System.currentTimeMillis();
        List<BccBasicInfo> list = bccBasicInfoService.findByRecordState(Bcc.RecordState.PUBLISH);
        BccShowControl bccShowControl = bccShowControlService.findByBccIdAndShowType(Bcc.BCC_DEDAULT, "price");
        int intervalHour = 24 / nodeNumDay;
        for (BccBasicInfo bccBasicInfo : list) {
            log.info("bccId:{}", bccBasicInfo.getBccId());
            String bccId = bccBasicInfo.getBccId();
            Calendar fr = Calendar.getInstance();
            fr.set(Calendar.HOUR_OF_DAY, 0);
            fr.set(Calendar.MINUTE, 0);
            fr.set(Calendar.SECOND, 0);
            fr.add(Calendar.DAY_OF_WEEK, -1);
            Calendar to = Calendar.getInstance();
            to.set(Calendar.HOUR_OF_DAY, 0);
            to.set(Calendar.MINUTE, 0);
            to.set(Calendar.SECOND, 0);

            to.add(Calendar.DAY_OF_WEEK, -1);
            to.add(Calendar.HOUR_OF_DAY, intervalHour);
            for (int j = 1; j <= nodeNumDay; j++) {
                BccPriceInfo priceInfo = bccPriceInfoService.findOneByBccIdBetween(bccId, fr.getTime(), to.getTime());
                if (priceInfo == null) {
                    continue;
                }
                BccPriceDay priceDay = new BccPriceDay();
                BeanUtils.copyProperties(priceInfo, priceDay);
                bccPriceDayService.save(priceDay);
                if (StringUtils.isNotEmpty(priceDay.getPriceBtc()) && StringUtils.isNotEmpty(priceDay.getPriceUsd()) && StringUtils.isNotEmpty(priceDay.getPriceCny()))
                    bccRedisService.cacheDayPrice(priceDay.getBccId(), getShowInfo(BeanSerial.Bean2Json(priceDay), bccShowControl));

                fr.add(Calendar.HOUR_OF_DAY, intervalHour);
                to.add(Calendar.HOUR_OF_DAY, intervalHour);
            }
        }
        bccPriceInfoService.delete2DayBefore();
        log.info("dayPriceTask end,consume:{} ms", (System.currentTimeMillis() - start));

    }
}
